<script setup lang="ts">
  import { ref, onMounted, computed } from 'vue'
  import { useGlobalStore } from '@/stores/global'

  const globalStore = useGlobalStore()

  // 用户信息（通过ensureUserInfo获取）
  const user = ref()

  // 打字机效果文本数组
  const typewriterTexts = computed(() => {
    // 优先使用个人简述，如果为空则使用完整描述并拆分
    const personalSummary = user.value?.personalSummary
    const description = user.value?.description || ''

    if (personalSummary) {
      // 如果有个人简述，直接使用（可以包含多个关键词，用空格分隔）
      return personalSummary.split(' ').filter((word: string) => word.length > 0)
    }

    // 回退到原有逻辑：拆分完整描述
    return description.split(' ').filter((word: string) => word.length > 0) || ['']
  })

  // 当前显示的文本索引（用于循环切换不同的文本）
  const currentTextIndex = ref(0)
  // 当前正在打字显示的文本内容
  const currentText = ref('')

  // 打字动画定时器引用，用于互斥和清理
  const typewriterTimer = ref<number | null>(null)

  // 启动打字机动画效果，带互斥控制
  const startTypewriter = (typewriterTexts: string[]) => {
    // 清理上一次动画
    if (typewriterTimer.value) {
      clearTimeout(typewriterTimer.value)
      typewriterTimer.value = null
    }
    let index = 0 // 当前字符索引
    let targetText = typewriterTexts[currentTextIndex.value] // 当前要显示的目标文本

    /**
     * 递归打字函数 - 逐字符显示文本
     */
    const type = () => {
      if (index < targetText.length) {
        // 还有字符需要显示，继续打字
        currentText.value = targetText.slice(0, index + 1)
        index++
        typewriterTimer.value = window.setTimeout(type, 100) // 100ms后显示下一个字符
      } else {
        // 当前文本打字完成，等待1秒后开始删除
        typewriterTimer.value = window.setTimeout(() => {
          /**
           * 递归删除函数 - 逐字符删除文本
           */
          const deleteText = () => {
            if (currentText.value.length > 0) {
              // 还有字符需要删除
              currentText.value = currentText.value.slice(0, -1)
              typewriterTimer.value = window.setTimeout(deleteText, 50) // 50ms后删除下一个字符（删除速度比打字快）
            } else {
              // 删除完成，切换到下一个文本
              currentTextIndex.value = (currentTextIndex.value + 1) % typewriterTexts.length
              targetText = typewriterTexts[currentTextIndex.value]
              index = 0 // 重置字符索引
              typewriterTimer.value = window.setTimeout(type, 500) // 500ms后开始打下一个文本
            }
          }
          typewriterTimer.value = window.setTimeout(deleteText, 2000) // 显示完整文本2秒后开始删除
        }, 1000)
      }
    }

    // 开始打字动画
    type()
  }

  // 社交链接（计算属性）
  const socialLinks = computed(() => {
    const links = []

    // GitHub
    if (user.value?.github) {
      links.push({
        name: 'GitHub',
        icon: 'icon-GitHub',
        iconType: 'svg',
        url: user.value.github,
        color: '#333',
      })
    }

    // Gitee
    if (user.value?.gitee) {
      links.push({
        name: 'Gitee',
        icon: 'icon-gitee',
        iconType: 'svg',
        url: user.value.gitee,
        color: '#C71D23',
      })
    }

    // Email
    if (user.value?.email) {
      links.push({
        name: 'Email',
        icon: '📧',
        iconType: 'emoji',
        url: `mailto:${user.value.email}`,
        color: '#EA4335',
      })
    }

    return links
  })

  // 只在挂载时执行一次打字机动画
  onMounted(async () => {
    user.value = await globalStore.ensureUserInfo()
    startTypewriter(typewriterTexts.value)
  })
</script>

<template>
  <section class="hero-section">
    <div class="hero-background">
      <!-- 动态背景粒子 -->
      <div class="particles">
        <div v-for="i in 50" :key="i" class="particle"></div>
      </div>

      <!-- 背景渐变 -->
      <div class="gradient-overlay"></div>
    </div>

    <div class="hero-content">
      <div class="container">
        <div class="hero-inner">
          <!-- 头像和基本信息 -->
          <div class="profile-section">
            <div class="avatar-container">
              <img :src="globalStore.avatarUrl || ''" :alt="user?.nickname" class="avatar" />
              <div class="avatar-ring"></div>
            </div>

            <div class="profile-info">
              <h1 class="name">{{ user?.nickname }}</h1>
              <div class="title-container">
                <span class="title-text">{{ currentText }}</span>
                <span class="cursor">|</span>
              </div>
              <p class="location">
                <span class="location-icon">📍</span>
                {{ user?.location || '中国' }}
              </p>
            </div>
          </div>

          <!-- 统计数据 -->
          <div class="stats-grid">
            <div class="stat-item">
              <div class="stat-number">{{ globalStore.siteStats.totalPosts }}</div>
              <div class="stat-label">文章</div>
            </div>
            <div class="stat-item">
              <div class="stat-number">{{ globalStore.siteStats.totalViews }}</div>
              <div class="stat-label">阅读</div>
            </div>
            <div class="stat-item">
              <div class="stat-number">{{ globalStore.siteStats.totalComments }}</div>
              <div class="stat-label">评论</div>
            </div>
            <div class="stat-item">
              <div class="stat-number">{{ globalStore.siteStats.runningDays }}</div>
              <div class="stat-label">天</div>
            </div>
          </div>

          <!-- 社交链接 -->
          <div class="social-links">
            <a
              v-for="social in socialLinks"
              :key="social.name"
              :href="social.url"
              :title="social.name"
              class="social-link"
              :style="{ '--hover-color': social.color }"
              target="_blank"
              rel="noopener noreferrer"
            >
              <span v-if="social.iconType === 'emoji'" class="social-icon">{{ social.icon }}</span>
              <svg v-else class="social-icon icon" aria-hidden="true">
                <use :xlink:href="`#${social.icon}`"></use>
              </svg>
            </a>
          </div>

          <!-- 向下滚动提示 -->
          <div class="scroll-hint">
            <div class="scroll-text">探索更多</div>
            <div class="scroll-arrow">↓</div>
          </div>
        </div>
      </div>
    </div>
  </section>
</template>

<style lang="scss" scoped>
  .hero-section {
    position: relative;
    min-height: 100vh;
    display: flex;
    align-items: center;
    overflow: hidden;
  }

  /* 背景层 */
  .hero-background {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1;
  }

  .particles {
    position: absolute;
    width: 100%;
    height: 100%;
  }

  .particle {
    position: absolute;
    width: 4px;
    height: 4px;
    background: var(--primary-color);
    border-radius: 50%;
    opacity: 0.6;
    animation: float 6s ease-in-out infinite;

    &:nth-child(odd) {
      animation-delay: -2s;
    }

    &:nth-child(3n) {
      animation-delay: -4s;
    }
  }

  /* 预定义的粒子位置，避免使用 random() 函数 */
  .particle:nth-child(1) {
    left: 10%;
    top: 20%;
    animation-duration: 4s;
    animation-delay: 0.1s;
  }

  .particle:nth-child(2) {
    left: 80%;
    top: 10%;
    animation-duration: 5s;
    animation-delay: 0.3s;
  }

  .particle:nth-child(3) {
    left: 30%;
    top: 70%;
    animation-duration: 3s;
    animation-delay: 0.5s;
  }

  .particle:nth-child(4) {
    left: 60%;
    top: 40%;
    animation-duration: 6s;
    animation-delay: 0.2s;
  }

  .particle:nth-child(5) {
    left: 90%;
    top: 80%;
    animation-duration: 4s;
    animation-delay: 0.8s;
  }

  .particle:nth-child(6) {
    left: 15%;
    top: 60%;
    animation-duration: 5s;
    animation-delay: 0.4s;
  }

  .particle:nth-child(7) {
    left: 75%;
    top: 25%;
    animation-duration: 3s;
    animation-delay: 0.7s;
  }

  .particle:nth-child(8) {
    left: 45%;
    top: 85%;
    animation-duration: 4s;
    animation-delay: 0.6s;
  }

  .particle:nth-child(9) {
    left: 25%;
    top: 15%;
    animation-duration: 5s;
    animation-delay: 0.9s;
  }

  .particle:nth-child(10) {
    left: 85%;
    top: 55%;
    animation-duration: 3s;
    animation-delay: 0.1s;
  }

  .particle:nth-child(11) {
    left: 5%;
    top: 45%;
    animation-duration: 6s;
    animation-delay: 1.2s;
  }

  .particle:nth-child(12) {
    left: 65%;
    top: 75%;
    animation-duration: 4s;
    animation-delay: 0.3s;
  }

  .particle:nth-child(13) {
    left: 35%;
    top: 35%;
    animation-duration: 5s;
    animation-delay: 0.8s;
  }

  .particle:nth-child(14) {
    left: 95%;
    top: 5%;
    animation-duration: 3s;
    animation-delay: 0.5s;
  }

  .particle:nth-child(15) {
    left: 55%;
    top: 95%;
    animation-duration: 4s;
    animation-delay: 1s;
  }

  .particle:nth-child(16) {
    left: 20%;
    top: 50%;
    animation-duration: 5s;
    animation-delay: 0.2s;
  }

  .particle:nth-child(17) {
    left: 70%;
    top: 30%;
    animation-duration: 6s;
    animation-delay: 0.7s;
  }

  .particle:nth-child(18) {
    left: 40%;
    top: 80%;
    animation-duration: 3s;
    animation-delay: 0.4s;
  }

  .particle:nth-child(19) {
    left: 50%;
    top: 10%;
    animation-duration: 4s;
    animation-delay: 0.9s;
  }

  .particle:nth-child(20) {
    left: 80%;
    top: 90%;
    animation-duration: 5s;
    animation-delay: 0.6s;
  }

  .particle:nth-child(21) {
    left: 12%;
    top: 65%;
    animation-duration: 3s;
    animation-delay: 1.1s;
  }

  .particle:nth-child(22) {
    left: 88%;
    top: 22%;
    animation-duration: 4s;
    animation-delay: 0.3s;
  }

  .particle:nth-child(23) {
    left: 33%;
    top: 88%;
    animation-duration: 5s;
    animation-delay: 0.8s;
  }

  .particle:nth-child(24) {
    left: 77%;
    top: 44%;
    animation-duration: 6s;
    animation-delay: 0.5s;
  }

  .particle:nth-child(25) {
    left: 8%;
    top: 77%;
    animation-duration: 3s;
    animation-delay: 1s;
  }

  .particle:nth-child(26) {
    left: 62%;
    top: 18%;
    animation-duration: 4s;
    animation-delay: 0.2s;
  }

  .particle:nth-child(27) {
    left: 38%;
    top: 62%;
    animation-duration: 5s;
    animation-delay: 0.7s;
  }

  .particle:nth-child(28) {
    left: 92%;
    top: 38%;
    animation-duration: 3s;
    animation-delay: 0.4s;
  }

  .particle:nth-child(29) {
    left: 18%;
    top: 92%;
    animation-duration: 4s;
    animation-delay: 0.9s;
  }

  .particle:nth-child(30) {
    left: 72%;
    top: 8%;
    animation-duration: 5s;
    animation-delay: 0.6s;
  }

  .particle:nth-child(31) {
    left: 28%;
    top: 72%;
    animation-duration: 6s;
    animation-delay: 1.2s;
  }

  .particle:nth-child(32) {
    left: 82%;
    top: 28%;
    animation-duration: 3s;
    animation-delay: 0.3s;
  }

  .particle:nth-child(33) {
    left: 48%;
    top: 82%;
    animation-duration: 4s;
    animation-delay: 0.8s;
  }

  .particle:nth-child(34) {
    left: 68%;
    top: 48%;
    animation-duration: 5s;
    animation-delay: 0.5s;
  }

  .particle:nth-child(35) {
    left: 22%;
    top: 68%;
    animation-duration: 3s;
    animation-delay: 1s;
  }

  .particle:nth-child(36) {
    left: 78%;
    top: 12%;
    animation-duration: 4s;
    animation-delay: 0.2s;
  }

  .particle:nth-child(37) {
    left: 42%;
    top: 78%;
    animation-duration: 5s;
    animation-delay: 0.7s;
  }

  .particle:nth-child(38) {
    left: 58%;
    top: 32%;
    animation-duration: 6s;
    animation-delay: 0.4s;
  }

  .particle:nth-child(39) {
    left: 32%;
    top: 58%;
    animation-duration: 3s;
    animation-delay: 0.9s;
  }

  .particle:nth-child(40) {
    left: 88%;
    top: 42%;
    animation-duration: 4s;
    animation-delay: 0.6s;
  }

  .particle:nth-child(41) {
    left: 14%;
    top: 88%;
    animation-duration: 5s;
    animation-delay: 1.1s;
  }

  .particle:nth-child(42) {
    left: 74%;
    top: 14%;
    animation-duration: 3s;
    animation-delay: 0.3s;
  }

  .particle:nth-child(43) {
    left: 44%;
    top: 74%;
    animation-duration: 4s;
    animation-delay: 0.8s;
  }

  .particle:nth-child(44) {
    left: 84%;
    top: 34%;
    animation-duration: 5s;
    animation-delay: 0.5s;
  }

  .particle:nth-child(45) {
    left: 24%;
    top: 84%;
    animation-duration: 6s;
    animation-delay: 1s;
  }

  .particle:nth-child(46) {
    left: 64%;
    top: 24%;
    animation-duration: 3s;
    animation-delay: 0.2s;
  }

  .particle:nth-child(47) {
    left: 36%;
    top: 64%;
    animation-duration: 4s;
    animation-delay: 0.7s;
  }

  .particle:nth-child(48) {
    left: 96%;
    top: 36%;
    animation-duration: 5s;
    animation-delay: 0.4s;
  }

  .particle:nth-child(49) {
    left: 16%;
    top: 96%;
    animation-duration: 3s;
    animation-delay: 0.9s;
  }

  .particle:nth-child(50) {
    left: 76%;
    top: 16%;
    animation-duration: 4s;
    animation-delay: 0.6s;
  }

  @keyframes float {
    0%,
    100% {
      transform: translateY(0px) rotate(0deg);
      opacity: 0.6;
    }

    50% {
      transform: translateY(-20px) rotate(180deg);
      opacity: 1;
    }
  }

  .gradient-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(
      135deg,
      rgba(59, 130, 246, 0.1) 0%,
      rgba(147, 51, 234, 0.1) 50%,
      rgba(219, 39, 119, 0.1) 100%
    );
  }

  /* 内容层 */
  .hero-content {
    position: relative;
    z-index: 2;
    width: 100%;
  }

  .hero-inner {
    text-align: center;
    max-width: 800px;
    margin: 0 auto;
  }

  /* 个人档案部分 */
  .profile-section {
    margin-bottom: 3rem;
  }

  .avatar-container {
    position: relative;
    display: inline-block;
    margin-bottom: 2rem;
  }

  .avatar {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    border: 4px solid var(--bg-color);
    box-shadow: var(--shadow-lg);
    transition: transform 0.3s ease;

    &:hover {
      transform: scale(1.05);
    }
  }

  .avatar-ring {
    position: absolute;
    top: -8px;
    left: -8px;
    right: -8px;
    bottom: -8px;
    border: 2px solid var(--primary-color);
    border-radius: 50%;
    opacity: 0.5;
    animation: pulse 2s ease-in-out infinite;
  }

  @keyframes pulse {
    0%,
    100% {
      transform: scale(1);
      opacity: 0.5;
    }

    50% {
      transform: scale(1.1);
      opacity: 0.3;
    }
  }

  .profile-info {
    .name {
      font-size: 2.5rem;
      font-weight: 700;
      color: var(--text-primary);
      margin: 0 0 1rem 0;
      background: linear-gradient(135deg, var(--primary-color), var(--primary-hover));
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      background-clip: text;
    }
  }

  .title-container {
    height: 2rem;
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 2px;
  }

  .title-text {
    font-size: 1.2rem;
    color: var(--text-secondary);
    font-weight: 500;
  }

  .cursor {
    color: var(--primary-color);
    animation: blink 1s infinite;
  }

  @keyframes blink {
    0%,
    50% {
      opacity: 1;
    }

    51%,
    100% {
      opacity: 0;
    }
  }

  .location {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    color: var(--text-tertiary);
    font-size: 1rem;
    margin: 0;

    .location-icon {
      font-size: 1.1rem;
    }
  }

  .blog-description {
    font-size: 1.1rem;
    color: var(--text-secondary);
    line-height: 1.6;
    margin-top: 1.5rem;
    max-width: 600px;
    margin-left: auto;
    margin-right: auto;
    opacity: 0.85;
  }

  /* 统计数据网格 */
  .stats-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 2rem;
    margin-bottom: 3rem;
    padding: 2rem;
    background: var(--bg-color);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-lg);
    backdrop-filter: blur(10px);
  }

  .stat-item {
    text-align: center;

    .stat-number {
      font-size: 2rem;
      font-weight: 700;
      color: var(--primary-color);
      line-height: 1;
      margin-bottom: 0.5rem;
    }

    .stat-label {
      font-size: 0.9rem;
      color: var(--text-secondary);
      font-weight: 500;
    }
  }

  /* 社交链接 */
  .social-links {
    display: flex;
    justify-content: center;
    gap: 1rem;
    margin-bottom: 6rem;
  }

  .social-link {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--bg-color);
    border: 2px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    transition: all 0.3s ease;
    box-shadow: var(--shadow-sm);
    position: relative;
    z-index: 3;

    &:hover {
      transform: translateY(-2px);
      border-color: var(--hover-color, var(--primary-color));
      box-shadow: var(--shadow-lg);
    }

    .social-icon {
      font-size: 1.2rem;

      &.icon {
        width: 1.2rem;
        height: 1.2rem;
        fill: currentColor;
      }
    }
  }

  /* 滚动提示 */
  .scroll-hint {
    position: absolute;
    bottom: 1rem;
    left: 50%;
    transform: translateX(-50%);
    text-align: center;
    color: var(--text-tertiary);
    animation: bounce 2s infinite;
    z-index: 2;

    .scroll-text {
      font-size: 0.9rem;
      margin-bottom: 0.5rem;
    }

    .scroll-arrow {
      font-size: 1.2rem;
    }
  }

  @keyframes bounce {
    0%,
    20%,
    53%,
    80%,
    100% {
      transform: translateX(-50%) translateY(0);
    }

    40%,
    43% {
      transform: translateX(-50%) translateY(-10px);
    }

    70% {
      transform: translateX(-50%) translateY(-5px);
    }
  }

  /* 响应式设计 */
  @media (max-width: 768px) {
    .hero-inner {
      padding: 2rem 1rem;
    }

    .profile-info .name {
      font-size: 2rem;
    }

    .title-text {
      font-size: 1rem;
    }

    .stats-grid {
      grid-template-columns: repeat(2, 1fr);
      gap: 1rem;
      padding: 1.5rem;
    }

    .stat-item .stat-number {
      font-size: 1.5rem;
    }

    .social-links {
      gap: 0.75rem;
      margin-bottom: 5rem;
    }

    .social-link {
      width: 40px;
      height: 40px;
    }

    .scroll-hint {
      bottom: 0.5rem;
    }
  }

  @media (max-width: 480px) {
    .avatar {
      width: 100px;
      height: 100px;
    }

    .profile-info .name {
      font-size: 1.8rem;
    }

    .stats-grid {
      padding: 1rem;
    }
  }
</style>
